<?php

namespace addons\shopro\model;

use app\admin\model\share\CommonShare;
use app\admin\model\share\Money as ShareMoney;
use app\common\model\ShareConfig;
use think\Db;
use think\Model;
use addons\shopro\exception\Exception;

/**
 * 购物车模型
 */
class ShareReward extends Model
{

    // 表名,不含前缀
    protected $name = 'share_reward';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'addtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    protected $hidden = [];

    // 追加属性
    protected $append = [];

    public function addProfitAmount($order)
    {

        if ($order->one_day == 1 || $order->one_day == 2) {

            $puser = '';
            $userinfo = Db::table('share_user')->where(['user_id' => $order->user_id])->find();
            $user_flags = Db("share_user_flags")->where(['user_id' => $order->user_id])->find();
            $list = Db::name('share_user')->where('is_sep_income', 1)->field('user_id')->select();
            foreach ($list as $v) {
                if (strpos($userinfo['role_url'], '/' . $v['user_id'] . '/') !== false) {
                    $puser = Db::table('share_user')->where(['user_id' => $userinfo['parent_id']])->find();
                    break;
                }
            }

            //\think\log::error(json_encode([$puser, $order->user_id]));
           /* if (isset($puser['role']) && $puser['role'] >= 5 && $puser['is_sep_income'] == 1) {
                $data[0]['user_id'] = $puser['user_id'];
                $data[1]['user_id'] = $puser['user_id'];
            }*/
//            \think\log::log(json_encode([$userinfo, $puser]));

            //file_put_contents('tt.txt',$con_ddata1['reward']."__".$con_ddata2['reward']);
            //$profitAmount = sprintf("%.2f", substr(sprintf("%.3f", $order->total_amount * 0.3), 0, -2));
            //$promoteUserAmount = sprintf("%.2f", $profitAmount * 0.8);
            if($order->one_day == 1){
                $con_ddata1 = Db::name("share_config")->where(['type' => 4,'role'=>1])->find();
                $con_ddata2 = Db::name("share_config")->where(['type' => 4,'role'=>2])->find();

                $promoteUserAmount = sprintf("%.2f", substr(sprintf("%.3f", $order->total_amount * $con_ddata1['reward']), 0, -2));
                $spokeUserAmount = sprintf("%.2f", substr(sprintf("%.3f", $order->total_amount * $con_ddata2['reward']), 0, -2));
                $one_star = 0;
                $two_star = 0;
            }elseif ($order->one_day == 2){//易购臻品.易购甄选
                $con_ddata1 = Db::name("share_config")->where(['type' => 5,'role'=>1])->find();
                $con_ddata2 = Db::name("share_config")->where(['type' => 5,'role'=>2])->find();
                $con_ddata3 = Db::name("share_config")->where(['type' => 5,'role'=>3])->find();
                $con_ddata4 = Db::name("share_config")->where(['type' => 5,'role'=>4])->find();

                $promoteUserAmount = sprintf("%.2f", substr(sprintf("%.3f", $order->total_amount * $con_ddata1['reward']), 0, -2));
                $spokeUserAmount = sprintf("%.2f", substr(sprintf("%.3f", $order->total_amount * $con_ddata2['reward']), 0, -2));
                $one_star = sprintf("%.2f", substr(sprintf("%.3f", $order->total_amount * $con_ddata3['reward']), 0, -2));
                $two_star = sprintf("%.2f", substr(sprintf("%.3f", $order->total_amount * $con_ddata4['reward']), 0, -2));
            }
            $in_arr = [];
            $ShareConfig1 = ShareConfig::where(['type' => 3, 'role' => 1])->find();
            $ShareConfig2 = ShareConfig::where(['type' => 3, 'role' => 2])->find();
            $ShareConfig3 = ShareConfig::where(['type' => 3, 'role' => 3])->find();
            $ShareConfig4 = ShareConfig::where(['type' => 3, 'role' => 4])->find();
            $pattern = Db::name("config")->where(['name'=>'pattern'])->value('value');
            if(isset($pattern) && $pattern == 2){
                $type1 = 7;
                $type2 = 8;
            }else{
                $type1 = 1;
                $type2 = 2;
                $type3 = 3;
                $type4 = 4;
            }
            if($promoteUserAmount > 0){
                $price_bl1 = 1-$ShareConfig1['reward'];
                $mutual_money_bl1 = $ShareConfig1['reward'];
                $data['order_id'] = $order->id;
                $data['status'] = 0;
                $data['type'] = $type1;
                $data['money'] = bcmul($price_bl1, $promoteUserAmount, 2);//$promoteUserAmount;
                $data['mutual_money'] = bcmul($mutual_money_bl1, $promoteUserAmount, 2);;
                $data['user_flags'] = isset($user_flags) ? $user_flags['user_flags'] : '';
                $data['addtime'] = time();
                $in_arr[] = $data;
            }
            if($spokeUserAmount > 0){
                $price_bl2 = 1-$ShareConfig2['reward'];
                $mutual_money_bl2 = $ShareConfig2['reward'];
                $data['order_id'] = $order->id;
                $data['status'] = 0;
                $data['type'] = $type2;
                $data['money'] = bcmul($price_bl2, $spokeUserAmount, 2);//$spokeUserAmount;
                $data['mutual_money'] = bcmul($mutual_money_bl2, $spokeUserAmount, 2);;
                $data['user_flags'] = isset($user_flags) ? $user_flags['user_flags'] : '';
                $data['addtime'] = time();
                $in_arr[] = $data;
            }
            if($one_star > 0){
                $price_bl3 = 1-$ShareConfig3['reward'];
                $mutual_money_bl3 = $ShareConfig3['reward'];
                $data['order_id'] = $order->id;
                $data['status'] = 0;
                $data['type'] = $type3;
                $data['money'] = bcmul($price_bl3, $promoteUserAmount, 2);//$promoteUserAmount;
                $data['mutual_money'] = bcmul($mutual_money_bl3, $promoteUserAmount, 2);;
                $data['user_flags'] = isset($user_flags) ? $user_flags['user_flags'] : '';
                $data['addtime'] = time();
                $in_arr[] = $data;
            }
            if($two_star > 0){
                $price_bl4 = 1-$ShareConfig4['reward'];
                $mutual_money_bl4 = $ShareConfig4['reward'];
                $data['order_id'] = $order->id;
                $data['status'] = 0;
                $data['type'] = $type4;
                $data['money'] = bcmul($price_bl4, $promoteUserAmount, 2);//$promoteUserAmount;
                $data['mutual_money'] = bcmul($mutual_money_bl4, $promoteUserAmount, 2);;
                $data['user_flags'] = isset($user_flags) ? $user_flags['user_flags'] : '';
                $data['addtime'] = time();
                $in_arr[] = $data;
            }
            if($spokeUserAmount > 0 || $promoteUserAmount > 0){
                $addRes = $this->insertAll($in_arr);
            }else{
                return ['code' => 1, 'msg' => '无须分润'];
            }

            if ($addRes) {
                return ['code' => 1, 'msg' => '分润成功'];
            }
            return ['code' => 0, 'msg' => '分润失败'];
        }
        return ['code' => 1, 'msg' => '不符合分润规则'];
    }
    public function addProfitAmountTwo($order)
    {

        if ($order->one_day == 2) {
            $userinfo = Db::table('share_user')->where(['user_id' => $order->user_id])->find();
            $userinfo_inviter = Db::table('share_user')->where(['user_id' => $userinfo['inviter_id']])->find();
            $this->add_money(1, sprintf("%.2f", $order->total_amount * 0.1), $order->id, $userinfo['inviter_id'], $userinfo_inviter['role'], $order->goods_id, 1);
            //
            $flags = Db::name("share_user_flags")->where(['user_id'=>$order->user_id])->find();
            if($flags){
                $userinfo_f = Db::table('share_user')->where(['user_flags' => $flags['user_flags']])->find();
                $this->add_money(1, sprintf("%.2f", $order->total_amount * 0.05), $order->id, $userinfo_f['user_id'], $userinfo_f['role'], $order->goods_id, 1);

            }else{

                $is_leader_data = Db::table('share_user')->where(['is_leader' => 1])->find();
                if($is_leader_data){
                    $this->add_money(1, sprintf("%.2f", $order->total_amount * 0.05), $order->id, $is_leader_data['user_id'], $is_leader_data['role'], $order->goods_id, 1);
                }
            }
        }
    }
    private function add_money($type, $money, $order_id, $user_id, $role, $goods_id, $is_one = 0)
    {
//        \think\log::error(json_encode([$type, $money, $order_id]));
        if (isset($money) && $money != "0.00") {
            //获取不通等级分给佣金的比例 qi11.3
            $con_ddata = Db::name("share_config")->where(['role' => $role, 'type' => 3])->find();
            //$user->price += bcmul(0.9, $money, 2);
            $price_bl = 1 - $con_ddata['reward'];
            $mutual_money_bl = $con_ddata['reward'];

            $ShareMoney = new ShareMoney();
            //$ShareMoney->money = $money;
            $ShareMoney->money = bcmul($price_bl, $money, 2);
            $ShareMoney->mutual_money = bcmul($mutual_money_bl, $money, 2);
            $ShareMoney->type = $type;
            $ShareMoney->order_id = $order_id;
            $ShareMoney->user_id = $user_id;
            $ShareMoney->goods_id = $goods_id;
            $ShareMoney->role = $role;
            $ShareMoney->addtime = time();
            if ($type == 1 || $type == 2) {
                $ShareMoney->is_grant = 0;
            }
            $ShareMoney->save();
        }
    }
}
